package model;

import java.awt.Point;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Path consists of points of which any unit can walk on, different speeds is
 * determined by the interval to call the path iterator.
 */
@SuppressWarnings("serial")
public class Path implements Iterable<Point>, Serializable {

	private ArrayList<Point> path = new ArrayList<Point>();
	private Point start, end;

	/**
	 * @return start point of path.
	 */
	public Point getStart() {
		return start;
	}

	/**
	 * @return end point 
	 */
	public Point getEnd() {
		return end;
	}

	/**
	 * Creates new empty path...
	 */
	public Path() {
	}

	/**
	 * Adds a point to the path.
	 */
	public void add(Point p) {
		if (path.isEmpty())
			start = p;
		else
			end = p;
		path.add(p);
	}

	/**
	 * Returns an iterator to the path, that can be used to "walk" along the
	 * path.
	 */
	public Iterator<Point> iterator() {
		return path.iterator();
	}

	/**
	 * clears all paths.
	 */
	public void clear() {
		path.clear();
	}

	public int size() {
		return path.size();
	}

	public void print() {
		for (int i = 0; i < path.size(); i++)
			System.out.println("x: " + path.get(i).x + " y: " + path.get(i).y);
	}

	public Path copyAndReverse() {
		Path tmp = new Path();
		for (int i = path.size() - 1; i >= 0; i--) {
			tmp.add(path.get(i));
		}
		return tmp;
	}

	public boolean equals(Path p) {
		return getStart().equals(p.getStart()) && getEnd().equals(p.getEnd());
	}

	public double getDistance() {
		double sum = 0;
		for (int i = 0; i < path.size() - 1; i++) {
			sum += path.get(i).distance(path.get(i + 1));
		}
		return sum;
	}
}
